package com.hiyee.goods.controller;

import com.hiyee.api.controller.BaseController;
import com.hiyee.api.controller.goods.GoodsControllerApi;
import com.hiyee.goods.service.CommentService;
import com.hiyee.goods.service.GoodsService;
import com.hiyee.pojo.Product;
import com.hiyee.pojo.auth.LoginCustomer;
import com.hiyee.pojo.bo.GoodsSearchBO;
import com.hiyee.pojo.repo.GoodsInfoRepo;
import com.hiyee.response.ResponseJsonResult;
import com.hiyee.response.PagedGridResult;
import com.hiyee.response.ResponseStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController extends BaseController implements GoodsControllerApi {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CommentService commentService;

    @Override
    public ResponseJsonResult search(String sortBy, String sortType, Integer categoryId, String name, Integer page, Integer pageSize, LoginCustomer loginCustomer) {
       GoodsSearchBO goodsSearchBO = new GoodsSearchBO();
       goodsSearchBO.setSortType(sortType);
       goodsSearchBO.setSortBy(sortBy);
       goodsSearchBO.setName(name);
       goodsSearchBO.setPage(page);
       goodsSearchBO.setPageSize(pageSize);
       goodsSearchBO.setCategoryId(categoryId);
       PagedGridResult pagedGridResult = goodsService.search(goodsSearchBO);
       return this.success(pagedGridResult);
    }

    @Override
    public ResponseJsonResult getInfo(Long id) {
        GoodsInfoRepo goodsInfo = goodsService.getInfo(id);
        if(goodsInfo == null) {
            return this.error(ResponseStatusEnum.GOODS_NOT_FOUND);
        }
        return this.success(goodsInfo);
    }

    @Override
    public ResponseJsonResult getServiceList(Long goodsId) {
        List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
        Map<String, Object> service = new HashMap<>();
        service.put("name", "七天无理由退货");
        service.put("summary", "满足相应条件时，消费者可申请7天无理由退货");
        serviceList.add(service);
        service.put("name", "48小时发货");
        service.put("summary", "下单后48小时之内发货");
        serviceList.add(service);
        return ResponseJsonResult.success(serviceList);
    }

    @Override
    public ResponseJsonResult getCommentList(Long goodsId, Integer page, Integer pageSize) {
        return this.success(commentService.getList(goodsId, page, pageSize));
    }

    @Override
    public ResponseJsonResult getProductInfo(Long productId) {
        Product product = goodsService.getProduct(productId);
        return this.success(product);
    }
}
